<template>
  <div class="app-container">
    <el-dialog title="选择生产计划" :visible.sync="open" width="1400px" v-dialogDrag append-to-body @close='close'>
      <!-- 搜索工作栏 -->
      <el-form :model="queryParams" ref="queryForm" size="small" v-show="showSearch" label-width="68px">
        <el-row>
          <el-col :span="4">
            <el-form-item label="工厂" prop="factoryId">
              <selectFactory v-model="queryParams.factoryId"></selectFactory>
            </el-form-item>
          </el-col>
          <el-col :span="4">
            <el-form-item label="生产车间" prop="workshopId">
              <selectWorkshop v-model="queryParams.workshopId"></selectWorkshop>
            </el-form-item>
          </el-col>
          <el-col :span="4">
            <el-form-item label="批次号" prop="batchNo">
              <el-input v-model="queryParams.batchNo" placeholder="请输入批次号" clearable @keyup.enter.native="handleQuery"/>
            </el-form-item>
          </el-col>
          <el-col :span="4">
            <el-form-item label="计划单号" prop="code">
              <el-input v-model="queryParams.code" placeholder="请输入计划单号" clearable @keyup.enter.native="handleQuery"/>
            </el-form-item>
          </el-col>
          <el-col :span="4">
            <el-form-item label="是否紧急" prop="emergent">
              <el-select v-model="queryParams.emergent" placeholder="请选择是否紧急" clearable size="small">
                <el-option v-for="dict in this.getDictDatas(DICT_TYPE.SYSTEM_INTEGER_STATUS)"
                           :key="dict.value" :label="dict.label" :value="dict.value"/>
              </el-select>
            </el-form-item>
          </el-col>
        </el-row>
        <el-row>
          <el-col :span="5">
            <el-form-item label="产品信息" prop="itemProduceCode">
              <el-input v-model="queryParams.itemProduceCode" placeholder="请选择产品生产信息" disabled >
                <el-button slot="append" icon="el-icon-search" @click="openSelectItemProduce= true"></el-button>
              </el-input>
            </el-form-item>
          </el-col>
          <el-col :span="5">
            <el-form-item label="开始时间" prop="planStartTime">
              <el-date-picker v-model="queryParams.planStartTime" style="width: 240px" value-format="yyyy-MM-dd HH:mm:ss" type="daterange"
                              range-separator="-" start-placeholder="开始日期" end-placeholder="结束日期" :default-time="['00:00:00', '23:59:59']" />
            </el-form-item>
          </el-col>
          <el-col :span="4">
            <el-form-item>
              <el-button type="primary" icon="el-icon-search" @click="handleQuery">搜索</el-button>
              <el-button icon="el-icon-refresh" @click="resetQuery">重置</el-button>
            </el-form-item>
          </el-col>
        </el-row>
      </el-form>
      <!-- 列表 -->
      <el-table v-loading="loading" :data="list" @selection-change="handleSelectionChange">
        <el-table-column type="selection" width="55"> </el-table-column>
        <el-table-column label="工厂" align="center" prop="factoryName" fixed />
        <el-table-column label="生产车间" align="center" prop="workshopName" fixed />
        <el-table-column label="批次号" align="center" prop="batchNo" fixed width="150px" />
        <el-table-column label="计划单号" align="center" prop="code" fixed width="180px" />
        <el-table-column label="产品名称" align="center" prop="itemName" width="150px" />
        <el-table-column label="产品代码" align="center" prop="itemCode" width="150px" />
        <el-table-column label="产品规格" align="center" prop="spec" width="150px" />
        <el-table-column label="产品图号" align="center" prop="drawCode" width="150px" />
        <el-table-column label="是否紧急" align="center" prop="emergent">
          <template v-slot="scope">
            <dict-tag :type="DICT_TYPE.SYSTEM_INTEGER_STATUS" :value="scope.row.emergent" />
          </template>
        </el-table-column>
        <el-table-column label="预计产量" align="center" prop="planAmount" />
        <el-table-column label="计划开始时间" align="center" prop="planStartTime" width="180">
          <template v-slot="scope">
            <span>{{ parseTime(scope.row.planStartTime) }}</span>
          </template>
        </el-table-column>
        <el-table-column label="计划完成时间" align="center" prop="planCompleteTime" width="180">
          <template v-slot="scope">
            <span>{{ parseTime(scope.row.planCompleteTime) }}</span>
          </template>
        </el-table-column>
        <el-table-column label="计划状态" align="center" prop="status">
          <template v-slot="scope">
            <dict-tag :type="DICT_TYPE.MES_PLAN_STATUS" :value="scope.row.status" />
          </template>
        </el-table-column>
      </el-table>
      <!-- 分页组件 -->
      <pagination v-show="total > 0" :total="total" :page.sync="queryParams.pageNo" :limit.sync="queryParams.pageSize" @pagination="getList"/>

      <div slot="footer" class="dialog-footer">
        <el-button type="primary" @click="selectPlan">确 定</el-button>
        <el-button @click="close">取 消</el-button>
      </div>
    </el-dialog>

    <SelectItemProduce :openSelectItemProduce.sync="openSelectItemProduce" @selectItemProduce="selectItemProduce"></SelectItemProduce>

  </div>
</template>

<script>
import { getPlanPage } from "@/api/mes/plan";
import SelectFactory from '@/components/SelectFactory/index.vue'
import SelectWorkshop from '@/components/SelectWorkshop/index.vue'
import SelectItemProduce from '@/components/SelectItemProduce/index.vue'
export default {
  name: "SelectPlan",
  components: { SelectItemProduce, SelectFactory, SelectWorkshop },
  props: {
    openSelectPlan: {
      required: true,
      default: false
    }
  },
  watch: {
    openSelectPlan(newVal) {
      this.open = newVal;
    }
  },
  data() {
    return {
      open: this.openSelectPlan,
      // 遮罩层
      loading: true,
      // 显示搜索条件
      showSearch: true,
      openSelectItemProduce: false,
      // 总条数
      total: 0,
      // 产品通用信息列表
      list: [],
      // 查询参数
      queryParams: {
        pageNo: 1,
        pageSize: 10,
        factoryId: null,
        workshopId: null,
        itemProduceId: null,
        itemProduceCode: null,
        batchNo: null,
        code: null,
        emergent: null,
        status: 1,
        planStartTime: [],
      },
      multipleSelection: [],
      mesPlanStatusOptions: [],
    }
  },
  created() {
    this.getList();
  },
  methods: {
    /** 查询列表 */
    getList() {
      this.loading = true;
      // 执行查询
      getPlanPage(this.queryParams).then(response => {
        this.list = response.data.list;
        this.total = response.data.total;
        this.loading = false;
      });
    },
    /** 搜索按钮操作 */
    handleQuery() {
      this.queryParams.pageNo = 1;
      this.getList();
    },
    /* 选择变化 */
    handleSelectionChange(val) {
      this.multipleSelection = val;
    },
    /** 重置按钮操作 */
    resetQuery() {
      this.resetForm("queryForm");
      this.handleQuery();
    },
    /** 选择产品 */
    selectPlan() {
      this.$emit('selectPlan', this.multipleSelection);
      this.close()
    },
    selectItemProduce(data) {
      console.log(data)
      this.queryParams.itemProduceId = data.id;
      this.queryParams.itemProduceCode = data.itemCode;
      this.openSelectItemProduce = false;
    },
    close() {
      this.open = false;
      this.$emit('update:openSelectPlan', this.open)
    }
  }
};
</script>

